<?php


namespace app\api\controller;

use app\admin\model\AdLog;
use app\admin\model\User;
use app\common\controller\Api;
use app\admin\model\Node;

class Advert extends Api
{
    protected $noNeedLogin = ['record','refuse'];
    //protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 广告记录
     *
     * @param int $uid  用户id
     * @param string $node 广告应用场景:0=看一次获得3次转盘广告,1=转盘奖励,2=离开转盘翻倍,3=签到广告,4=拒绝5次的插屏广告
     * @param int $adtype 广告类型：1激励，2插屏，3banner
     * @param int $come 广告来源:0=游戏,1=SDK
     * @param int $multiple 倍率
     * @param int $trigger  是否触发:0=否,1=是
     * @param int $play 是否播放成功:0=否,1=是
     * @param int $status 是否请求成功:0=否,1=是
     */
    public function record()
    {
        $this->checkParam(['node','adtype','come','trigger','play','status']);
        //$this->checkParam('token', 'head');
        //$token = $this->request->header('token');
        $uid = $this->auth->id;
        //$uid = 1;
        $user = User::where('id', $uid)->find();
        //$user = User::all();
       // $user = user::get($token);
        $node = $this->request->param('node');
        $nodeRes = Node::where('node_id', $node)->find();
        if (!$nodeRes) {
            $this->error('Invalid node');
        }
        $adtype = $this->request->param('adtype');
        $come = $this->request->param('come');
        $multiple = $this->request->param('multiple');
        //$multiple = empty($multiple) ? null : $multiple;
        $trigger = $this->request->param('trigger');
        $play = $this->request->param('play');
        $status = $this->request->param('status');
        $data = [
            'user_id' => $user['id'],
            'app_id' => $user['app_id'],
            'come' => $come,
            'adtype' => $adtype,
            'node' => $node,
            'multiple' => $multiple,
            'trigger' => $trigger,
            'status' => $status,
            'play' => $play,
            'addtime' => date('Y-m-d H:i:s',time())
        ];
        AdLog::insert($data);
        /*dump($this->request->param());*/
        $this->success(__('successful'));
    }
    /**
     * 拒绝插屏广告
     *
     * @param int $uid  用户id
     */
    public function refuse()
    {

        /*$token = $this->request->header('token');
        $user = Token::get($token);*/
        $uid = $this->auth->id;
        $reset = $this->request->param('reset');
        $user = User::where('id', $uid)->find();
        if ($reset) {
            User::where('id',$user['id'])->update(['refuse' => 0]);
        } else {
            if ($user['refuse'] == 5) {
                User::where('id',$user['id'])->update(['refuse' => 1]);
            } else {
                User::where('id',$user['id'])->update(['refuse' => $user['refuse'] +1]);
            }
        }
        $user = User::where('id', $uid)->find();
        $this->success(__('successful'), ['refuse_count' => $user['refuse']]);
    }
}